package com.yequan.thread.lock.reentrantlock.multicontion;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {

    private Lock lock=new ReentrantLock();
    private Condition conditionA=lock.newCondition();
    private Condition conditionB=lock.newCondition();

    public void awaitA(){
        try {
            lock.lock();
            System.out.println("lock A 前时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
            conditionA.await();
            System.out.println("lock A 释放时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void awaitB (){
        try {
            lock.lock();
            System.out.println("lock B 前时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
            conditionB.await();
            System.out.println("lock B 释放时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signalAll_A(){
        try{
            lock.lock();
            System.out.println("signalAll A 时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
            conditionA.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public void signalAll_B(){
        try{
            lock.lock();
            System.out.println("signalAll B 时间"+System.currentTimeMillis()+"--->"+Thread.currentThread().getName());
            conditionB.signalAll();
        } finally {
            lock.unlock();
        }
    }
}
